
package com.kugou.game.sdk.download;

import com.kugou.download.DownloadFile;
import com.kugou.download.IProgressListener;
import com.kugou.download.manager.ParamsWrapper;
import com.kugou.framework.download.IDownloadService;
import com.kugou.framework.download.IDownloadServiceDestoryListener;

import android.content.ComponentName;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 下载服务调用工具类
 * 
 * @author chenys
 */
public class DownloadServiceUtil {

    private static IDownloadService sService;

    private static HashMap<Context, ServiceBinder> sConnectionMap = new HashMap<Context, ServiceBinder>();

    private static ArrayList<IProgressListener> sWattingTasks = new ArrayList<IProgressListener>();

    public static class DownloadServiceToken {
        ContextWrapper mWrappedContext;

        DownloadServiceToken(ContextWrapper context) {
            mWrappedContext = context;
        }
    }

    public static interface OnDownloadServiceConnectionListener {
        void onDownloadServiceConnection();
    }

    private static class ServiceBinder implements ServiceConnection {

        private OnDownloadServiceConnectionListener mOnDownloadServiceConnectionListener;

        public ServiceBinder(OnDownloadServiceConnectionListener listener) {
            mOnDownloadServiceConnectionListener = listener;
        }

        public void onServiceConnected(ComponentName className, IBinder service) {
            if (service != null && service instanceof IDownloadService) {
                sService = (IDownloadService) service;
                if (mOnDownloadServiceConnectionListener != null) {
                    mOnDownloadServiceConnectionListener.onDownloadServiceConnection();
                }
                if (sWattingTasks != null && sWattingTasks.size() > 0) {
                    for (IProgressListener listener : sWattingTasks) {
                        sService.addListener(listener);
                    }
                    sWattingTasks.clear();
                }
            }
        }

        public void onServiceDisconnected(ComponentName className) {
            sService = null;
        }
    }

    /**
     * 绑定服务
     * 
     * @param context
     * @param listener
     * @return
     */
    public static DownloadServiceToken bindToService(Context context,
            OnDownloadServiceConnectionListener listener) {
        ContextWrapper cw = new ContextWrapper(context);
        cw.startService(new Intent(cw, DownloadService.class));
        ServiceBinder sb = new ServiceBinder(listener);
        if (cw.bindService((new Intent()).setClass(cw, DownloadService.class), sb, 0)) {
            sConnectionMap.put(cw, sb);
            return new DownloadServiceToken(cw);
        }
        return null;
    }

    public static void unbindFromService(DownloadServiceToken token) {
        if (token == null) {
            return;
        }
        ContextWrapper cw = token.mWrappedContext;
        ServiceBinder sb = sConnectionMap.remove(cw);
        if (sb == null) {
            return;
        }
        cw.unbindService(sb);
        if (sConnectionMap.isEmpty()) {
            sService = null;
        }
    }

    private static boolean checkServiceBinded() {
        boolean isRunning = sService != null;
        return isRunning;
    }

    public static boolean isInitialized() {
        return sService != null;
    }

    /**
     * 下载
     * 
     * @param resUrl
     * @param filePath
     * @param listener
     * @return
     */
    public static boolean download(String resUrl, String filePath, IProgressListener listener) {
        if (checkServiceBinded()) {
            return sService.download(resUrl, filePath, listener);
        }
        return false;
    }

    public static boolean download(String resUrl, String filePath, int classId,
            IProgressListener listener) {
        if (checkServiceBinded()) {
            return sService.download(resUrl, filePath, classId, listener);
        }
        return false;
    }

    public static boolean download(String key, String resUrl, String filePath, int classId,
            IProgressListener listener) {
        if (checkServiceBinded()) {
            return sService.download(key, resUrl, filePath, classId, listener);
        }
        return false;
    }

    public static boolean download(String key, String resUrl, String filePath, long fileSize,
            int classId, IProgressListener listener) {
        if (checkServiceBinded()) {
            return sService.download(key, resUrl, filePath, fileSize, classId, listener);
        }
        return false;
    }

    public static boolean download(String key, String resUrl, String filePath, String fileName,
            long fileSize, int classId, IProgressListener listener) {
        if (checkServiceBinded()) {
            return sService.download(key, resUrl, filePath, fileName, fileSize, classId, listener);
        }
        return false;
    }

    public static boolean download(ParamsWrapper paramsWrapper, IProgressListener listener) {
        if (checkServiceBinded()) {
            return sService.download(paramsWrapper, listener);
        }
        return false;
    }

    public static boolean addToWaittingQueue(ParamsWrapper paramsWrapper, IProgressListener listener) {
        if (checkServiceBinded()) {
            return sService.addToWaittingQueue(paramsWrapper, listener);
        }
        return false;
    }

    public static void stopDownload(String key) {
        if (checkServiceBinded()) {
            sService.stopDownload(key);
        }
    }

    public static void removeFromWaittingQueue(String key) {
        if (checkServiceBinded()) {
            sService.removeFromWaittingQueue(key);
        }
    }

    public static boolean isDownloading(String key) {
        if (checkServiceBinded()) {
            return sService.isDownloading(key);
        }
        return false;
    }

    public static boolean isInWaittingQueue(String key) {
        if (checkServiceBinded()) {
            return sService.isInWaittingQueue(key);
        }
        return false;
    }

    public static DownloadFile getDownloadFile(String key) {
        if (checkServiceBinded()) {
            return sService.getDownloadFile(key);
        }
        return null;
    }

    public static int getProgress(String key) {
        if (checkServiceBinded()) {
            return sService.getProgress(key);
        }
        return 0;
    }

    public static void removeListener(IProgressListener listener) {
        if (checkServiceBinded()) {
            sService.removeListener(listener);
        }
    }

    public static void addListener(IProgressListener listener) {
        if (checkServiceBinded()) {
            sService.addListener(listener);
        } else {
            if (!sWattingTasks.contains(listener)) {
                sWattingTasks.add(listener);
            }
        }
    }

    public static void setGolbalListener(IProgressListener listener) {
        if (checkServiceBinded()) {
            sService.setGolbalListener(listener);
        } else {
            if (!sWattingTasks.contains(listener)) {
                sWattingTasks.add(listener);
            }
        }
    }

    public static void cancelDownload(DownloadFile downloadFile, boolean realDelete) {
        if (checkServiceBinded()) {
            sService.cancelDownload(downloadFile, realDelete);
        }
    }

    public static void stopAll() {
        if (checkServiceBinded()) {
            sService.stopAll();
        }
    }

    public static void setOnDownloadServiceDestoryListener(IDownloadServiceDestoryListener listener) {
        if (checkServiceBinded()) {
            sService.setOnDownloadServiceDestoryListener(listener);
        }
    }

    public static DownloadFile getDownloadFileFromCache(String key) {
        if (checkServiceBinded()) {
            return sService.getDownloadFileFromCache(key);
        } else {
            return null;
        }
    }

    public static int getDownloadingSize() {
        if (checkServiceBinded()) {
            return sService.getDownloadingSize();
        } else {
            return 0;
        }
    }

    public static int getWaittingSize() {
        if (checkServiceBinded()) {
            return sService.getWaittingSize();
        } else {
            return 0;
        }
    }

    public static void setMaxDownloadingNum(int maxDownloadingNum) {
        if (checkServiceBinded()) {
            sService.setMaxDownloadingNum(maxDownloadingNum);
        }
    }

    public static ArrayList<DownloadFile> getDownloadingFiles() {
        if (checkServiceBinded()) {
            return sService.getDownloadingFiles();
        } else {
            return null;
        }
    }

    public static ArrayList<DownloadFile> getWaittingFiles() {
        if (checkServiceBinded()) {
            return sService.getWaittingFiles();
        } else {
            return null;
        }
    }

    public static ArrayList<DownloadFile> getDownloadFilesFromCache() {
        if (checkServiceBinded()) {
            return sService.getDownloadFilesFromCache();
        } else {
            return null;
        }
    }

    public static void removeDownloadFileFromCache(String key) {
        if (checkServiceBinded()) {
            sService.removeDownloadFileFromCache(key);
        }
    }

}
